﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WiimoteLib;

namespace WiiPresenter {
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window {
        SysTray mSysTray = new SysTray();
        enum State { PPT, Zoom };
        State CurrentState;
        Wiimote mWiimote;
        delegate void AnonDelegate();
        DispatcherTimer mTimer = new DispatcherTimer();

        static private MainWindow mCurrentWindow;
        public static MainWindow CurrentWindow {
            get { return MainWindow.mCurrentWindow; }
        }

        public MainWindow() {
            InitializeComponent();
            mCurrentWindow = this;

            mTimer.Tick += new EventHandler(mTimer_Tick);
            mTimer.Interval = new TimeSpan(0, 0, 0, 0, 25);

            StateChanged += new EventHandler(Window1_StateChanged);
            Closed += new EventHandler(MainWindow_Closed);
            butnConnect.Click +=new RoutedEventHandler(butnConnect_Click);
        }

        void mTimer_Tick(object sender, EventArgs e) {
            // Could still be in queue and processed after its disposed
            if (mWiimote != null) {
                WiimoteState xState = mWiimote.WiimoteState;
                //TODO: Probably change this to see waht windows are active instad of state, ie route to what is active
                if (CurrentState == State.Zoom && ZoomWindow.CurrentWindow == null) {
                    // User could have pressed escape key
                    SetState(State.PPT);
                }
                switch (CurrentState) {
                    case State.PPT: {
                            HandlePPT(xState);
                            break;
                        }
                    case State.Zoom: {
                            ZoomWindow.CurrentWindow.WiimoteStatus(xState);
                            break;
                        }
                }
            }
        }

        void MainWindow_Closed(object sender, EventArgs e) {
            // Must dispose, if not when we run again it hangs unless the BT is disconnected and reconnected.
            Disconnect();
            // To remove our icon from the system tray
            mSysTray.Dispose();
        }

        void PushKeys(string aKeys) {
            System.Windows.Forms.SendKeys.SendWait(aKeys);
        }

        void Window1_StateChanged(object sender, EventArgs e) {
            if (WindowState == WindowState.Minimized) {
                Hide();
            }
        }

        public void Restore() {
            Show();
            WindowState = WindowState.Normal;
        }

        void Connect() {
            mWiimote = new Wiimote();
            mWiimote.AltWriteMethod = chckUseAlternateWriteMethod.IsChecked.Value;
            mWiimote.Connect();

            butnConnect.Content = mSysTray.mitmConnect.Text = "Disconnect";
            chckUseAlternateWriteMethod.IsEnabled = false;

            // Currently we dont use the Accel input so we turn it off
            //mWiimote.SetReportType(Wiimote.InputReport.IRAccel, true);
            // Also currently we dont display the battery level either. :)
            //mWiimote.GetBatteryLevel();
            SetState(State.PPT);
            mTimer.Start();
        }

        void Disconnect() {
            if (mWiimote != null) {
                mTimer.Stop();
                mWiimote.Dispose();
                mWiimote = null;

                butnConnect.Content = mSysTray.mitmConnect.Text = "Connect";
                chckUseAlternateWriteMethod.IsEnabled = true;
            }
        }

        void butnConnect_Click(object sender, RoutedEventArgs e) {
            if (mWiimote == null) {
                Connect();
            } else {
                Disconnect();
            }
        }

        void SetState(State aNewState) {
            CurrentState = aNewState;
            mWiimote.SetRumble(true);
            System.Threading.Thread.Sleep(50);
            mWiimote.SetRumble(false);
            switch (CurrentState) {
                case State.PPT: {
                        mWiimote.SetLEDs(true, false, false, false);
                        break;
                    }
                case State.Zoom: {
                        mWiimote.SetLEDs(false, true, false, false);
                        break;
                    }
            }
        }

        private bool fHandlePPT_DoPlay = true;
        private void HandlePPT(WiimoteState aState) {
            if (aState.ButtonState.A) {
                PushKeys("{Left}");
            }
            if (aState.ButtonState.B) {
                PushKeys("{Right}");
            }

            if (aState.ButtonState.Minus) {
                PushKeys("B"); // Blank
            }
            if (aState.ButtonState.Home) {
                if (fHandlePPT_DoPlay) {
                    PushKeys("{Esc}");
                } else {
                    PushKeys("+{F5}");
                }
                fHandlePPT_DoPlay = !fHandlePPT_DoPlay;
            }
            if (aState.ButtonState.Plus) {
                ZoomWindow.Start();
                SetState(State.Zoom);
            }

        }

    }
}
